package ui.list 
{
	import com.GCInventory;
	import com.GCItem;
	import data.GCItemData;
	import events.GCEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCItemScrollList extends ScrollList
	{
		private var _inventory:GCInventory;					// Inventory currently connected to this list
		private var _filterType:uint;						// A filter for showing only item of this type
		private var capacityMeter:Sprite;
		private var capacityText:TextField;
		
		/**
		 * Constructor
		 * @param	width			Width of the scroll list
		 * @param	height			Height of the scroll list
		 */

		public function GCItemScrollList(width:int, height:int, showCapacity:Boolean = false, inventory:GCInventory = null) 
		{
			super(width, height, showCapacity);
			
			
			_inventory = inventory;
			loadInventory(inventory);
			_filterType = GCItemData.ITEM_TYPE_GENERAL;
			
		}
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------
		
		/**
		 * Loads the items in the given inventory into the list
		 * @param	inventory
		 */
		public function loadInventory(inventory:GCInventory):void
		{
			// Remove all previous items
			removeAll();
			_inventory = inventory;
			_displayList = new Dictionary();
			
			// Fill the list with all the items in the inventory
			if (_inventory != null)
			{
				var item:GCItem;
				var itemUI:GCListUI;
				if (_inventory.inventory != null)
				{
					for each (item in _inventory.inventory)
					{
						itemUI = new GCListUI(item);
						addDisplay(itemUI);
					}
				}
				_capacityText.text = "Capacity: " + _inventory.currentCapcity + "/" + _inventory.maxCapacity;
			}
		}		
		
		/**
		 * Adds the given itemUI to the list
		 * @param	newItem
		 */
		public function addItem(newItem:GCItem):void
		{			
			// Check for the item in your inventory
			if (_inventory != null)
			{
				var items:Dictionary = _inventory.inventory;
				var item:GCItem = items[newItem.itemId];
				
				if (item != null)
				{
				// It exists
					// Update the existing quantity
					item.quantity += newItem.quantity;
					
					// Check if the item exists in the visual
					var itemUI:GCListUI = _displayList[item];
					if (itemUI != null)
					{
						// Visual exists; Update visual quantity
						itemUI.changeQuantity(item.quantity);
					}
					else 
					{
						// Visual doesn't exist; Create a new UI
						itemUI = new GCListUI(item);
						addDisplay(itemUI);
					}
				}
				else
				{
					// Item doesn't exist; Create the new item					
					items[newItem.itemId] = newItem;
					var newItemUI:GCListUI = new GCListUI(newItem);
					addDisplay(newItemUI);
				}
				
				// Update the capacity of the inventory
				_inventory.currentCapcity += newItem.quantity;
				_capacityText.text = "Capacity: " + _inventory.currentCapcity + "/" + _inventory.maxCapacity;
			}
			else 
			{
				// We don't have an inventory
				return; // Or should we add to visual anyway?
			}			
		}
		

		
		/**
		 * Removes the given itemUI from the list if it is
		 * contained in the list
		 * @param	item
		 */
		public function removeItem(item:GCItem):void
		{
			if (item == null)
				return;
			var inventory:Dictionary = _inventory.inventory;
			if (_inventory != null)
			{
				// Check that the item is in the inventory
				if (inventory[item.itemId] == null)	
				{
					// Item doesn't exist; exit
					return;
				} 
				else 
				{
					//Item does exist; Remove item from inventory
					delete inventory[item.itemId];
					_inventory.currentCapcity -= item.quantity;
				}
				
				// Check for the display
				var itemUI:GCListUI = _displayList[item];
				if(itemUI == null)
				{
					// Display doesn't exist; exit
					return;
				} 
				else 
				{
					// Display exists; Remove the display
					removeDisplay(itemUI);					
				}
				
			}
			
			
			// Check that the item is in the inventory
				// Item doesn't exist; exit
				
				// Item does exist
				// Remove item from the inventory
				
				// Check for the display
					// Display doesn't exist; exit
					
					// Display exists
					// Remove Display
					// Update the list
				// Unselect item
			// Redraw
		}
		

		/**
		 * Clears out all the items in the list
		 */
		override public function removeAll():void
		{
			super.removeAll();
			_inventory = null;
			_displayList = null;
			_selected = null;
		}
		
		/**
		 * Returns the remaining capacity of this inventory
		 * @return
		 */
		public function remainingCapacity():int
		{
			return _inventory.maxCapacity - _inventory.currentCapcity;
		}
		
		/**
		 * Returns the amount of items currently stored
		 * @return
		 */
		public function getCapacity():int
		{
			return _inventory.currentCapcity;
		}
		
		/**
		 * Adds a filter to display
		 * @param	filterId
		 */
		public function setFilter(filterId:uint):void
		{
			_filterType = filterId;
			
			if (_filterType != GCItemData.ITEM_TYPE_GENERAL)
			{
				super.removeAll();
				loadInventory(_inventory);
			}
		}
		
		/**
		 * Redraws the list
		 */
		public function redraw():void
		{
			loadInventory(_inventory);
		}
		
		/**
		 * Realigns the UI components
		 */
		override public function realign():void
		{
			super.realign();
			_capacityText.text = "Capacity: " + _inventory.currentCapcity + "/" + _inventory.maxCapacity;		
		}
		
		
		//----------------------------------------------------------------------
		//	PRIVATE FUNCTIONS
		//----------------------------------------------------------------------
		
		/**
		 * Adds the new itemUI to the display
		 * @param	newItem
		 */
		private function addDisplay(newItem:GCListUI):void
		{
			// Check if the item is going to be filtered
			if (_filterType != GCItemData.ITEM_TYPE_GENERAL && _filterType != GCItemData.getTypeFromId(newItem.reference.itemId))
			{
				// This item is filtered from display
				return;
			}
			
			// XY Positioning
			newItem.x = verticalPadding;
			newItem.y = size * (verticalPadding + itemHeight);
			
			// Add listeners to the UI
			newItem.addEventListener(MouseEvent.ROLL_OVER, handleRollOver);
			newItem.addEventListener(MouseEvent.ROLL_OUT, handleRollOut);
			newItem.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			newItem.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);		
			
			// Update the list properties
			size++;
			listHeight = size * (verticalPadding + itemHeight);// - hitAreaHeight;
			
			// Remove the UI from the list
			_list.addChild(newItem);
			
			// Add the itemUI to the display list
			_displayList[newItem.reference] = newItem
		}
		
		/**
		 * Removes the given itemUI from the display
		 * @param	itemUI
		 */
		private function removeDisplay(itemUI:GCListUI):void
		{
			if (itemUI == null)
				return;
				
			// Check that the item is from this list
			if (_list != itemUI.parent)
				return;
			
			// Remove the listeners on the item
			itemUI.removeEventListener(MouseEvent.ROLL_OVER, handleRollOver);
			itemUI.removeEventListener(MouseEvent.ROLL_OUT, handleRollOut);
			itemUI.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			itemUI.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			
			// Remove the UI from the display
			_list.removeChild(itemUI);
			
			// Update the list properties
			size--;
			listHeight = size * (verticalPadding + itemHeight);// - hitAreaHeight;
			
			// Unselect item
			_selected = null;
			
			// Realign the list
			realign();
			
			// Remove the item from the display list
			delete _displayList[itemUI.reference];
		}

		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------
		
		/*
		override protected function handleMouseDown(event:MouseEvent):void
		{
			super.handleMouseDown(event);
			
			var object:GCListUI = event.currentTarget as GCListUI;
			
			// Show that the item is selected
			_selected = object;			
			
			//dispatchEvent(new GCEvent(GCEvent.LIST_ITEM_SELECTED_EVENT));
		}
		
		override protected function handleMouseUp(event:MouseEvent):void
		{
			super.handleMouseUp(event);
		}
		*/
		
	}
}